package com.tuanzi.loan.web.bean.member;

import com.tuanzi.loan.business.service.member.LoanMemberPriceService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.member.LoanMemberPriceRequest;
import com.tuanzi.loan.vo.response.member.LoanMemberPriceResponse;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.member.LoanMemberPriceLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.bean.ViewScoped;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanMemberPriceListBean extends JSFListingSessionBean<LoanMemberPriceRequest, LoanMemberPriceResponse> {

    @Autowired
    private LoanMemberPriceService loanMemberPriceService;


    private static final long serialVersionUID = -7505485021549263653L;

    @Override
    protected String page() {
        return "/views/member/loan-member-price-list.xhtml";
    }

    @Override
    protected void initLazyDataModel(){
        $ = new LoanMemberPriceRequest();
        lazyDataModel = new LoanMemberPriceLazyDataModel($, session, loanMemberPriceService);
    }

    public void onReset(){
        initLazyDataModel();
    }

    public String onAdd(){
        return redirect("/views/member/loan-member-price-info.xhtml");
    }

    public String onEdit(Long id){
       if(null == id){
           initLazyDataModel();
           return null;
       }

       return redirect("/views/member/loan-member-price-info.xhtml?id=" +id);
    }

    public void onDelete(Long id){
        try {
            loanMemberPriceService.delete(id);
            addSuccessfullyMessage();
            onReset();
        } catch (Exception e) {
            addErrorMessage("删除失败");
        }
    }

}
